<template>
  <div class="app-container">
    <div
      class="plan-description"
      ref="descriptionRef"
      :style="`margin-bottom:10px !important`"
    >
      <p>基础数据</p>
      <div class="plan-detail_line">
        <div class="plan-detail">
          <span class="bg-[#34d1ca]"></span>
          <span>计划编号</span>
          <span>{{ formData.planCode }}</span>
        </div>
        <div class="plan-detail">
          <span class="bg-[#151DFF]"></span>
          <span>计划名称</span>
          <span>{{ formData.planName }}</span>
        </div>
        <div class="plan-detail">
          <span class="bg-[#165DFF]"></span>
          <span>管理部门</span>
          <span>{{ formData.deviceDepartmentName }}</span>
        </div>
        <div class="plan-detail">
          <span class="bg-[#722ED1]"></span>
          <span>维修执行部门</span>
          <span>{{ formData.repairDepartmentName }}</span>
        </div>
        <div class="plan-detail">
          <span class="bg-[#722ED1]"></span>
          <span>作业时间</span>
          <span>{{ formData.planStartTime }}至{{ formData.planEndTime }}</span>
        </div>
      </div>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button
          v-if="formData.planStatus === 0"
          type="success"
          plain
          @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-tooltip v-else>
          <template #content> 计划已生成工单，无法操作。 </template>
          <el-button disabled type="success" plain @click="openDialog()"
            ><i-ep-plus />新增</el-button
          >
        </el-tooltip>
        <!-- <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        > -->
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" width="55" align="center" />

        <el-table-column
          label="设备编码"
          prop="assetCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备名称"
          prop="assetName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备种类"
          prop="assetTypeName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="品牌型号"
          prop="assetBrandName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ scope.row.assetModelNo }}{{ scope.row.assetModelNo ? "-" : ""
            }}{{ scope.row.assetBrandName }}
          </template>
        </el-table-column>
        <el-table-column
          label="维修类型"
          prop="repairType"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("PlanRepairType", scope.row.repairType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="维修班组"
          prop="executeTeamName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="维修人员"
          prop="locationName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{
              scope.row.executeUsers?.map((item) => item.personName).join(",")
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="设备位置"
          prop="locationName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="计划维修内容"
          prop="remarks"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划开始时间"
          prop="planStartTime"
          min-width="150"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{
              scope.row.planStartTime
                ? dayjs(scope.row.planStartTime).format("YYYY-MM-DD")
                : ""
            }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="240" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              v-if="formData.planStatus === 0"
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-tooltip v-else>
              <template #content> 计划已生成工单，无法操作。 </template>
              <el-button
                disabled
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
                ><i-ep-edit />编辑</el-button
              >
            </el-tooltip>

            <el-button
              v-if="formData.planStatus === 0"
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
            <el-tooltip v-else>
              <template #content> 计划已生成工单，无法操作。 </template>
              <el-button
                disabled
                type="primary"
                size="small"
                link
                @click="deleteItem(scope.row.id)"
                ><i-ep-delete />删除</el-button
              >
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <common-repairplandetail-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : getTableList())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>
<!-- 手动挂载左侧菜单高亮 -->
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  beforeRouteEnter(to, from, next) {
    to.meta.defaultActive = "/eam/equ/eamrepairplan";
    if (!to.query.id) {
      to.meta.title = "新增计划";
    } else {
      to.meta.title = "编辑计划";
    }

    next();
  },
});
</script>
<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useRouter, useRoute } from "vue-router";
import dayjs from "dayjs";
const router = useRouter();
const route = useRoute();
defineOptions({
  name: "COMMONREPAIRPLANDETAIL",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/eam/eamrepairplandetail/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  planId: route.query.id,
  assetKind: route.query.assetKind,
};

const deleteParam = {
  api: "/eam/equrepairplandetail/delete",
  data: { assetKind: route.query.assetKind },
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/eam/eamrepairplandetail/get/${id}`,
    method: "get",
    data: { assetKind: route.query.assetKind },
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开计划维修明细表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改计划维修明细";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增计划维修明细";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开计划维修明细表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "计划维修明细详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await request({
      url: `/eam/eamrepairplandetail/deleteItem/${id}`,
      method: "post",
      data: { assetKind: route.query.assetKind },
    }).then((res) => {
      ElMessage.success("删除成功！");
      search();
    });
  });
}

onMounted(() => {
  if (route.query.id) {
    getDangerTaskInfoList(route.query.id);
  }
  getTableList();
});
let formData: any = ref({});
const getDangerTaskInfoList = async (id) => {
  await request({
    url: `/eam/equrepairplan/get/${id}`,
    method: "get",
    data: { assetKind: route.query.assetKind },
  })
    .then((res) => {
      if (res.code === 200) {
        formData.value = res.data;
      }
    })
    .catch((res) => {
      console.log(res);
    });
};
</script>
<style scoped lang="scss">
@import url(@/styles/keywork/detail.scss);
.search-container {
  :deep(.el-form-item__label) {
    width: 120px !important;
  }
}
:deep(.el-radio-button__inner) {
  width: 90px;
  text-align: center;
}

.plan-description {
  background-color: rgb(238, 246, 253);
  padding: 15px;
  border-radius: 6px;
  margin-bottom: 18px;
  p {
    font-size: 14px;
    color: black;
    height: 21px;
    margin: 0 0 10px 0;
  }
  .plan-detail_line {
    overflow: hidden;
    height: 18px;
    display: flex;
    .plan-detail {
      font-size: 12px;
      overflow: hidden;
      span {
        display: block;
        float: left;
      }
      span:nth-child(1) {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        margin-right: 8px;
        margin-top: 3px;
      }
      span:nth-child(2) {
        color: #4e5969;
        margin-right: 20px;
      }
      span:nth-child(3) {
        color: #101010;
      }
    }
    .plan-detail:nth-child(1) {
      flex: 0.5;
    }
    .plan-detail:nth-child(2) {
      flex: 0.8;
    }
    .plan-detail:nth-child(3) {
      flex: 0.5;
    }
    .plan-detail:nth-child(4) {
      flex: 0.7;
    }
  }
}
</style>
